define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'xiluhouse/rent/rental/index' + location.search,
                    add_url: 'xiluhouse/rent/rental/add',
                    edit_url: 'xiluhouse/rent/rental/edit',
                    del_url: 'xiluhouse/rent/rental/del',
                    multi_url: 'xiluhouse/rent/rental/multi',
                    village_url: "xiluhouse/rent/village/index",
                    broker_url: "xiluhouse/user/user/index",
                    housetype_url: 'xiluhouse/house/housetype/index',
                    housetag_url: 'xiluhouse/house/housetag/index',
                    dragsort_url:'',
                    table: 'rental',
                }
            });

            var table = $("#table");
            table.on('post-common-search.bs.table', function (event, table) {
                var form = $("form", table.$commonsearch);
                $("input[name='house_type_id']", form).addClass("selectpage").data("source", $.fn.bootstrapTable.defaults.extend.housetype_url).data("primaryKey", "id").data("field", "name");
                $("input[name='village_id']", form).addClass("selectpage").data("source", $.fn.bootstrapTable.defaults.extend.village_url).data("primaryKey", "id").data("field", "name");
                $("input[name='broker_id']", form).addClass("selectpage").data("source", $.fn.bootstrapTable.defaults.extend.broker_url).data("primaryKey", "id").data("field", "mobile").data("params", function (obj) {
                    return {custom: {'userprofile.is_broker': 2}};
                });
                $("input[name='house_tag_ids']", form).addClass("selectpage").data("source", $.fn.bootstrapTable.defaults.extend.housetag_url).data("primaryKey", "id").data("field", "name").data("params", function (obj) {
                    return {custom: {classify: 2}};
                });
            })
            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'weigh',
                //启用固定列
                fixedColumns: true,
                //固定右侧列数
                fixedRightNumber: 1,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id'),sortable:true},
                        {field: 'name', title: __('租房基本信息'),operate:'LIKE',formatter: function (value, row, index) {
                            var html='';
                            html+='<div>'+ row.name +'</div>';
                            html+='<div>'+ row.price+'元/㎡  '+ row.space+'㎡ '+'</div>';
                            html+='<div>'+__('Face')+':'+ row.face +'</div>';
                            return html;
                        }},
                        {field: 'image', title: __('Image'), events: Table.api.events.image, formatter: Table.api.formatter.image,operate:false},
                        // {field: 'village_id', title: __('Village_id'),formatter: function (value, row, index) {
                        //     if(row.village_id>0){
                        //         var html='';
                        //         var url =$.fn.bootstrapTable.defaults.extend.village_url+ "?id=" + row.village_id;
                        //         html+='<a  href="' + url + '" class="bg-info btn-dialog" title="查看关联信息"> 【'+ row.village_id +'】'+row.village.name+' </a>';
                        //         return html;
                        //     }else{
                        //         return '-';
                        //     }
                        // }
                        // },
                        {field: 'village', title: __('小区'),operate:'LIKE'},
                        {field: 'user_id', title: __('User_id'),formatter: function (value, row, index) {
                            if(row.user_id>0){
                                var html='';
                                var url =$.fn.bootstrapTable.defaults.extend.user_url+ "?id=" + row.user_id;
                                html+='<a  href="' + url + '" class="bg-info btn-dialog" title="查看关联信息"> 【'+ row.user_id +'】'+row.user.nickname+'<br/>'+row.user.mobile+' </a>';
                                return html;
                            }else{
                                return '-';
                            }
                        }
                        },
                        {field: 'state', title: __('State'), searchList: {"1":__('State 1'),"2":__('State 2')}, formatter: Table.api.formatter.normal},
                        {field: 'is_public', title: __('Is_public'), searchList: {"0":__('Is_public 0'),"1":__('Is_public 1')}, formatter: Table.api.formatter.normal},
                        {field: 'price', title: __('Price'),sortable:true, operate:'BETWEEN',visible:false},
                        {field: 'room_num', title: __('Room_num'), operate:'BETWEEN',visible:false},
                        {field: 'ting_num', title: __('Ting_num'), operate:'BETWEEN',visible:false},
                        {field: 'wei_num', title: __('Wei_num'), operate:'BETWEEN',visible:false},
                        {field: 'floor_num', title: __('Floor_num'), operate:'BETWEEN',visible:false},
                        {field: 'space', title: __('Space'), operate:'BETWEEN',visible:false},
                        {field: 'face', title: __('Face'),operate:'LIKE',visible:false},
                        // {field: 'village.prov', title: __('Prov'),operate:'LIKE',formatter: function (value, row, index) {
                        //     var html='';
                        //     html+='<div>'+__('Prov')+':'+ row.village.prov +'</div>';
                        //     html+='<div>'+__('City')+':'+row.village.city+'</div>';
                        //     html+='<div>'+__('Region')+':'+row.village.region+'</div>';
                        //     html+='<div>'+__('Street')+':'+row.village.street+'</div>';
                        //     html+='<div>'+__('Addr')+':'+row.village.addr+'</div>';
                        //     return html;
                        // }},
                        // {field: 'village.city', title: __('City'),operate:'LIKE',visible:false},
                        // {field: 'village.region', title: __('Region'),operate:'LIKE',visible:false},
                        // {field: 'village.street', title: __('Street'),operate:'LIKE',visible:false},
                        // {field: 'village.addr', title: __('Addr'),operate:'LIKE',visible:false},
                        // {field: 'village.lat', title: __('Lat'),visible:false},
                        // {field: 'village.lng', title: __('Lng'),visible:false},
                        // // {field: 'subway_platform_id', title: __('Subway_platform_id')},
                        // // {field: 'subway_ids', title: __('Subway_ids')},
                        // {field: 'village.subway_distance', title: __('Subway_distance'), operate:'BETWEEN',visible:false},
                        // {field: 'village.subway_distance_text', title: __('Subway_distance_text'),operate:'LIKE',visible:false},
                        {field: 'house_type_id', title: __('House_type_id'),formatter: function (value, row, index) {
                            if(row.house_type_id>0){
                                var html='';
                                var url =$.fn.bootstrapTable.defaults.extend.housetype_url+ "?id=" + row.house_type_id;
                                html+='<div>'+__('House_type_id')+':'+ row.house_type_id +'</div>';
                                html+='<div>'+__('Housetype.name')+':'+row.housetype.name+'</div>';
                                html+='<div><a  href="' + url + '" class="btn btn-xs btn-primary btn-dialog" title="查看关联信息"> 查看关联信息 </a></div>';
                                return html;
                            }else{
                                return '-';
                            }
                        }},
                        { field: 'house_tag_ids', title: __('House_tag_ids'),operate:'FIND_IN_SET',formatter: function (value, row, index) {
                            if (row.house_tag_ids_text_arr) {
                                // house_tag_ids_text_arr
                                return Table.api.formatter.flag(row.house_tag_ids_text_arr, row, index);
                            }else{
                                return '-';
                            }
                        }},
                        // {field: 'village.nearby_bus_num', title: __('Nearby_bus_num'), operate:'BETWEEN',visible:false},
                        // {field: 'village.nearby_subway_num', title: __('Nearby_subway_num'), operate:'BETWEEN',visible:false},
                        // {field: 'village.nearby_shop_num', title: __('Nearby_shop_num'), operate:'BETWEEN',visible:false},
                        // {field: 'village.nearby_hospital_num', title: __('Nearby_hospital_num'), operate:'BETWEEN',visible:false},
                        {field: 'view_num', title: __('View_num'), operate:'BETWEEN',visible:false},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'updatetime', title: __('Updatetime'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'weigh', title: __('Weigh'),operate:false,sortable:true},
                        {field: 'status', title: __('Status'), searchList: {"normal":__('Status normal'),"hidden":__('Status hidden')}, formatter: Table.api.formatter.status},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        recyclebin: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    'dragsort_url': ''
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: 'xiluhouse/rent/rental/recyclebin' + location.search,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'name', title: __('Name'), align: 'left'},
                        {
                            field: 'deletetime',
                            title: __('Deletetime'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            formatter: Table.api.formatter.datetime
                        },
                        {
                            field: 'operate',
                            width: '130px',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            buttons: [
                                {
                                    name: 'Restore',
                                    text: __('Restore'),
                                    classname: 'btn btn-xs btn-info btn-ajax btn-restoreit',
                                    icon: 'fa fa-rotate-left',
                                    url: 'xiluhouse/rent/rental/restore',
                                    refresh: true
                                },
                                {
                                    name: 'Destroy',
                                    text: __('Destroy'),
                                    classname: 'btn btn-xs btn-danger btn-ajax btn-destroyit',
                                    icon: 'fa fa-times',
                                    url: 'xiluhouse/rent/rental/destroy',
                                    refresh: true
                                }
                            ],
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                var tag_type = Config.tag_type;
                for (var i=0;i<tag_type.length;i++){
                    var id = tag_type[i].id;
                    selectpagePrams(i,id);
                }


                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
function selectpagePrams(i,id) {
    $("#c-house_tag_ids" + i).data("params", function (obj) {
        //obj为SelectPage对象
        return {custom: {classify: 2, status: 'normal', tag_type_id: id}};
    });
}